<?php
/**
 *
 * 定时采集che168二手车,车主信息导入到车主表里
 * 采集规则：只采1天前的数据，入采集表不排重
 *
 */
#error_reporting ( 0 );
//ini_set("display_errors","ON");
//error_reporting(E_ALL ^ E_WARNING);
date_default_timezone_set ( "Asia/Shanghai" );
set_time_limit ( 0 );
if (isset ( $_SERVER ['REQUEST_URI'] )) {
    die ( 'error' );
}
if (! defined ( 'API_ROOT_PATH' )) {
    define ( 'API_ROOT_PATH', dirname ( __FILE__ ) );
}

require_once (API_ROOT_PATH . '/../inc/Config.php');
require_once (API_ROOT_PATH . '/../inc/dblink.php');
require_once (API_ROOT_PATH . '/../inc/Mysql.php');
//require_once (API_ROOT_PATH . '/Snoopy.class.php');

global $db;

$db = new Mysql ( MYSQLDB, MYSQLDB_USER, MYSQLDB_PWD, MYSQLDB_DATABASE );
//--------------------------------------------------------------
while ( true ) {

    $param = array();
    $param['channelid'] = 'xiaomistore';//'360';
    $param['pagesize'] = '24';//最大30
    $param['pageindex'] = '1';
    $city = 'bj';

    if($city=='bj'){//北京
       $pid = "110000";
       $cid = "110100";
    }elseif($city=='lf'){//廊坊
       $pid = "130000";
       $cid = "131000";
    }

/*
    $url = "http://apps.api.che168.com/phone/v51/cars/search.ashx?_appid=2scapp.android&_sign=AF4C27C10452F29AFD3BDCB1738B1DC6&appversion=4.8.5&areaid=0&brandid=0&channelid=".$param['channelid']."&cid=".$cid."&dealertype=9&ispic=0&orderby=4&pageindex=".$param['pageindex']."&pagesize=".$param['pagesize']."&pid=".$pid."&seriesid=0&sourceid=1&specid=0";
    $params = getparams($url);
    $newParams = '';
    $sign = getsign($params,$newParams);
    $newurl = getnewurl($url,$newParams,$sign);
    $data=file_get_contents($newurl);
    $rs = json_decode($data);

    $pages = $rs->result->pagecount;
    //echo "共".$pages."页\n";
    $rs = $db->fetchOne("SELECT comefrom_last_page FROM au_car_owner_co WHERE comefrom=0 ORDER BY owner_co_id DESC LIMIT 1");
    $last_page = $rs['comefrom_last_page'];
    $pages = $pages-$last_page+1;
*/
    //echo "本次采集开始于：".date("Y-m-d H:i:s")."\n";
    $pages = 30;
   //过滤省份
    for($i=1;$i<=$pages;$i++){
        //echo "获取第".$i."页内的符合条件的URL\n";
        if($city=='bj'){//北京
            $pid = "110000";
            $cid = "110100";
        }elseif($city=='lf'){//廊坊
            $pid = "130000";
            $cid = "131000";
        }
        $url = "http://apps.api.che168.com/phone/v51/cars/search.ashx?_appid=2scapp.android&_sign=AF4C27C10452F29AFD3BDCB1738B1DC6&appversion=4.8.5&areaid=0&brandid=0&channelid=".$param['channelid']."&cid=".$cid."&dealertype=9&ispic=0&orderby=4&pageindex=".$i."&pagesize=".$param['pagesize']."&pid=".$pid."&seriesid=0&sourceid=1&specid=0";

        //echo "解析URL参数\n";
        $params = getparams($url);
        $newParams = '';
        //echo "生成新的签名\n";
        $sign = getsign($params,$newParams);
        //echo "生成新的URL\n";
        $newurl = getnewurl($url,$newParams,$sign);
        //echo "获取新URL的页面内容\n";
        //$data=file_get_contents($newurl);
        curl($newurl,$data);
        $rs = json_decode($data);
        if(!$rs || $rs->returncode>0 || empty($rs->result)){
           echo 'c:'.$rs->returncode."\n";
            continue;
        }

        if(is_array($rs->result->carlist)){
            //echo "开始遍历URL\n";
            foreach($rs->result->carlist as $k=>$carobj){

                if($carobj->dealertype == '5'){ //已售出
                    continue;
                }
                //110000 北京 1130000 廊坊
                if(!in_array($carobj->cid,array('110100','131000'))){
                    continue;
                }

                //只采1天前的数据
                if((strtotime($carobj->publishdate)+1*3600*24)<time()){
                    continue;
                }

                $url = "http://apps.api.che168.com/phone/v48/cars/getcarinfodetail.ashx?_appid=2scapp.web&appversion=4.8&channelid=web&carid=".$carobj->carid;
                //echo '开始获取'.$i.'页面内容URL:'.$url."\n";
                getdetail($url,$carobj->carid);
                usleep(500000);
            }
        }

    }
    //echo "本次采集结束于：".date("Y-m-d H:i:s")."\n";
    //睡觉--------------------------------
    $sleeptimes = getNextSleepSecond();
    print_r("sleep:".$sleeptimes."\n");
    //echo $sql."\n";
    sleep($sleeptimes);
    continue;



}
//-----------------函数-----------------------------------------
//计算下次启动采集时间
function  getNextSleepSecond() {
    $date1 = time();
    $date2 = date('Y-m-d H:i:00',strtotime('+1 hour',$date1));
    $date2 = strtotime($date2);
    $s = $date2-$date1;    
    
    $date1 = date('Y-m-d H:i:s',$date1);
    $date2 = date('Y-m-d H:i:00',$date2);

    print_r("currtime:$date1   nexttime:$date2   timediff:$s\n");

    return $s;

}
//保存数据
function save($data=array()) {
    global $db;
    $data['master_id'] = 0;//主品牌ID
    $data['series_id'] = 0;//系列ID
    $data['status'] = 1;//0无效 1有效
    $data['created_at'] = date('Y-m-d H:i:s');

    $res = false;

    //只采1天前，不排重
    $sql = "SELECT owner_co_id FROM au_car_owner_co WHERE phone='".$data['phone']."' AND comefrom_createtime > '".date('Y-m-d')."'";
    $rs = $db->fetchOne($sql);
    if($rs){
        //echo $data['name'].' '.$data['phone'].'已存在于au_car_owner表'."\n";
        return;
    }
    if($data['phone']){
        try{
            $vals = array();
            foreach($data as $k=>$v){
                array_push($vals,$k.'=\''.addslashes(trim($v)).'\'');
            }
            $insert_sql = "INSERT `au_car_owner_co` SET ".implode(',', $vals);
            $res = $db->query($insert_sql);
        }catch(\Exception $e){
            echo $e->getMessage()."\n";
        }
    }else{
        //echo "\n\n……………………………………\n\n";
        //echo "\$rs[owner_co_id]=".$rs['owner_co_id'] .' \$data[phone]='.$data['phone'].' \$data[series]='. $data['series']."\n";
        //echo "\n\n\n\n";
    }

    if(!$res) {
        //echo $data['name'].' '.$data['phone'].'保存失败'."\n";
    }else{
        //echo $data['name'].' '.$data['phone'].'保存成功'."\n";
    }
}
//获取页面代码
function getdetail($url){
        $params = getparams($url);
        $newParams = '';
        $sign = getsign($params,$newParams);
        $newurl = getnewurl($url,$newParams,$sign);
        //$data = file_get_contents($newurl);
        curl($newurl,$data);

        $rs = json_decode($data);
        if(!$rs || $rs->returncode>0 || empty($rs->result)){
           echo 'c:'.$rs->returncode."\n";
            return;
        }
        echo ".\n";
        $insert = array();
        $insert['series'] = $rs->result->carname;
        $insert['name'] = $rs->result->salesperson->salesname;
        $insert['phone'] = preg_match("/1\d{10}/",$rs->result->salesperson->salesphone)?$rs->result->salesperson->salesphone:'';


        $insert['comefromurl'] = 'http://www.che168.com/personal/'.$rs->result->carid.'.html#pvareaid='.$rs->result->provinceid;
        $insert['comefromid'] = $rs->result->carid;
        $insert['comefrom'] = '0';//来源

        $insert['register_at'] = date('Y-m-d H:i:s',strtotime($rs->result->firstregtime));//上牌时间
        $insert['mileage'] = $rs->result->drivemileage;//行驶里程

        $insert['comefrom_province'] = $rs->result->provinceName;//原始省份
        $insert['comefrom_city'] = $rs->result->cityName;//原始城市
        $insert['comefrom_pid'] = $rs->result->provinceid;//原始省份id
        $insert['comefrom_cid'] = $rs->result->cityid;//原始城市id

        $insert['comefrom_createtime'] = $rs->result->publicdate;
        //echo "开始保存采集数据\n";
        if($insert['phone'] && $insert['series']){
            save($insert);
        }else{
            //echo "未匹配到电话信息------------即将采集下一个\n\n\n";
        }

}
//生成新的内容URL
function getnewurl($url,$newParams,$sign){
         $parseurl = parse_url($url);
         $newurl = $parseurl['scheme'].'://'.$parseurl['host'].$parseurl['path'].'?'.implode('&',$newParams).'&_sign='.$sign;
         return $newurl;
}
//获取URL参数
function getparams($url){
         $parseurl = parse_url($url);
         $params   = $parseurl['query'];
         $params   = explode('&',$params);
         return $params;
}
//--------Md5("com.che168.www"+(参数名+参数值)+(参数名+参数值)+...+"com.che168.www")
//生成新的签名
function getsign($params=array(),&$newParams){
    $md5 = 'com.che168.www';
    sort($params);
    $newParams = array();
    foreach($params as $p){
        if(strstr($p,'_sign'))continue;
        $md5 .= ''.$p;
        $newParams[]=$p;
    }
    $md5 .= 'com.che168.www';
    $md5 = str_replace('=','',$md5);
    $sign = strtoupper(md5($md5));
    return $sign;
}

function curl($url,&$output){
    //初始化
    $ch = curl_init();

    //设置选项，包括URL
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    //执行并获取HTML文档内容
    $output = curl_exec($ch);

    //释放curl句柄
    curl_close($ch);

}
